package xyz.scootaloo.oj.offer

import org.junit.Test
import xyz.scootaloo.console.app.support.Tester

/**
 * @author flutterdash@qq.com
 * @since 2021/4/13 10:06
 */
class P61 {

    private fun isStraight(nums: IntArray): Boolean {
        nums.sort()
        var laizi = 0
        var missing = 0
        for (idx in 0 until nums.size) {
            if (nums[idx] == 0) {
                laizi++
            } else {
                if (idx > 0 && nums[idx - 1] != 0) {
                    val interval = nums[idx] - nums[idx - 1]
                    missing += if (interval == 0) return false else interval - 1
                }
            }
        }

        return missing - laizi <= 0
    }

    @Test
    fun test0() {
        Tester.createTest(this::isStraight)
            .addCase(intArrayOf(1, 2, 3, 4, 5), true)
            .addCase(intArrayOf(0, 1, 4, 5, 0), true)
            .addCase(intArrayOf(0, 1, 3, 5, 6), false)
            .addCase(intArrayOf(11, 8, 12, 8, 10), false)
            .addCase(intArrayOf(11, 0, 9, 0, 0), true)
            .addCase(intArrayOf(0, 12, 11, 11, 0), false)
            .testAndShow()
    }

}
